<!-- 充值 -->
<template>
	<view class="container">
		<u-navbar autoBack title="充值金币" @rightClick="toPages('/pages/tabbar/mine/record')"
			:bgColor="'rgba(255, 255, 255,'+ navOpacity+')'" :leftIconColor="navOpacity >= 0.8 ? '#000' : '#fff'"
			:titleStyle="{color:navOpacity >= 0.8 ? '#000' : '#fff',fontSize:'40rpx'}" leftIconSize="46rpx" placeholder>
			<template slot="right">
				<view class="nav_right" :style="{color:navOpacity > 0.8 ? '#000' : '#fff'}">收支记录</view>
			</template>
		</u-navbar>
		<view class="num">
			<view class="money">{{userInfo && userInfo.coins || 0}}</view>
			<view class="num_title">金币余额</view>
		</view>
		<view class="content pd32">
			<u-row justify="justify" gutter="10" style="flex-wrap: wrap;">
				<template v-for="(item,i) in packageList">
					<u-col span="4" :key="i">
						<view class="select" :class="activeIndex == i ? 'selected' : '' "
							@click="handleItemActive(item,i)">
							<view class="s_money">{{item.coins || 0}}</view>
							<!-- <view class="title1">送40金币</view> -->
							<!-- <view class="title1">送7天骑士会员</view> -->
							<view class="rmb">&yen;{{item.price || 0}}</view>
							<!-- <view class="th">首充特惠</view> -->
						</view>
					</u-col>
				</template>
			</u-row>

			<view class="custom_inpuit">
				<u--input placeholder="自定义金额" border="surround" type="number" v-model="inputValue"
					maxlength="7"></u--input>
				<view class="c_rmb">
					&yen;
				</view>
			</view>

			<view class="recharge_way">
				<view class="way_title">充值方式</view>
				<view class="wat_lits choose" :class="payWay == 1 ? 'chooseed' : ''" @click="payWay = 1">
					<view class="image">
						
					</view>
					<view class="name">微信支付</view>
				</view>
				<view class="wat_lits choose" :class="payWay == 2 ? 'chooseed' : ''" @click="payWay = 2">
					<view class="image">
						
					</view>
					<view class="name">支付宝</view>
				</view>
			</view>
			<view class="bottom_btn">
				<view class="btn_box" @click="handlePay">立即充值</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		coinPackages,
		coinRecharge,
		userInfo
	} from "@/api/user.js"
	import {
		regPositiveNumber
	} from "@/util/regulars.js"

	const navHeight = uni.upx2px(88) + uni.getSystemInfoSync().statusBarHeight;
	export default {
		data() {
			return {
				inputValue: "",
				packageList: [],
				activeIndex: -1,
				payWay: 1,
				userInfo: null,
				navOpacity: 0
			}
		},
		onLoad() {
			// this.reqUserInfo();
			// this.reqCoinPackages();
		},
		methods: {
			async reqUserInfo() {
				const result = await userInfo()
				this.userInfo = result.data || {}
			},

			async reqCoinPackages() {
				const result = await coinPackages()
				this.packageList = result.data || []
			},

			/**
			 * @param {String} channel：方式，支付宝-alipay, 微信-wxpay
			 * @param {Number} package_id：选择套餐则传套餐id
			 */
			async handlePay() {
				if (this.activeIndex == -1 && this.isEmpty(this.inputValue)) {
					uni.showToast({
						title: "请选择充值金额，或输入金额",
						icon: "none"
					})
					return
				}
				if (!this.isEmpty(this.inputValue) && !regPositiveNumber.test(this.inputValue)) {
					uni.showToast({
						title: "请输入正整数金额",
						icon: "none"
					})
					return
				}
				uni.showLoading({
					title: "支付中...",
					mask: true
				})

				const params = {
					channel: this.payWay == 1 ? "wxpay" : "alipay",
				}
				if (!this.isEmpty(this.inputValue)) {
					params["custom"] = this.inputValue
				} else {
					params["package_id"] = this.packageList[this.activeIndex].id
				}
				const result = await coinRecharge(params)

				getApp().launchPay({
					result,
					payWay: this.payWay
				}).then(() => {
					this.reqUserInfo()
				})
			},

			handleItemActive(item, index) {
				this.activeIndex = index
			},
			clear() {
				this.value = '';
			},
			onPageScroll(val) {
				this.navOpacity = val.scrollTop / navHeight / 2
			}
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		height: 100%;
		background-size: 100% 386rpx;

		.nav_right {
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}

		.num {
			margin-top: 90rpx;
			display: flex;
			align-items: center;
			flex-direction: column;

			.money {
				font-size: 52rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}

			.num_title {
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				color: #FFFFFF;
			}
		}

		.content {
			margin-top: 80rpx;

			.select {
				height: 222rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border: 1rpx solid #FE5680;
				border-radius: 16rpx;
				margin-bottom: 20rpx;
				position: relative;

				.s_money {
					font-size: 48rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #FE5680;
				}

				.title1 {
					font-size: 24rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #6D6D6D;
					height: 35rpx;
				}

				.rmb {
					font-size: 24rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #FE5680;
				}

				.th {
					position: absolute;
					width: 120rpx;
					height: 40rpx;
					font-size: 24rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 40rpx;
					background-color: #FE5680;
					text-align: center;
					border-radius: 16rpx 0px 16rpx 0px;
					left: -2rpx;
					top: -2rpx;
				}
			}

			.selected {
				background: #FFEDED;
			}

			.custom_inpuit {
				display: flex;
				background-color: #FFEDED;
				border-radius: 32rpx;
				align-items: center;
				margin-top: 30rpx;

				.c_rmb {
					margin-right: 20rpx;
					font-size: 28rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #FE5680;
				}
			}

			.recharge_way {
				margin-top: 40rpx;

				.way_title {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #303030;
					margin-bottom: 32rpx;
				}

				.wat_lits {
					display: flex;
					align-items: center;
					margin-bottom: 50rpx;

					.image {
						width: 44rpx;
						height: 44rpx;
						margin-right: 20rpx;

						image {
							width: 44rpx;
							height: 44rpx;
						}
					}

					.name {
						font-size: 28rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #303030;
						line-height: 44rpx;
						flex: 1;
					}
				}
			}

			.bottom_btn {
				display: flex;
				justify-content: center;
				margin-top: 40rpx;
				margin-bottom: 40rpx;

				.btn_box {
					width: 464rpx;
					height: 76rpx;
					background: #FE5680;
					border-radius: 22rpx;
					text-align: center;
					line-height: 76rpx;
					color: #FFFFFF;
				}
			}
		}
	}
</style>